//
// Created by 张兮兮 on 2025/3/13.
//

#include "VideoWidget.h"
#include <QRandomGenerator>

VideoWidget::VideoWidget(QWidget *parent) {
    socketWidget = new SocketWidget();
    QPushButton *button = new QPushButton("Destroy B", this);
    button->setGeometry(10, 10, 100, 30);
    connect(button, &QPushButton::clicked, this, &VideoWidget::destroyBWidget);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &VideoWidget::renderFrame);
    timer->start(33); // ~30 FPS
}

void VideoWidget::renderFrame() {
    int r = QRandomGenerator::global()->bounded(256);  // 红色
    int g = QRandomGenerator::global()->bounded(256);  // 绿色
    int b = QRandomGenerator::global()->bounded(256);  // 蓝色

    currentFrame = QImage(800, 600, QImage::Format_RGB32);
    currentFrame.fill(QColor(r, g, b)); // 用随机颜色填充帧
    update();
}


void VideoWidget::destroyBWidget() {
    if (socketWidget) {
        delete socketWidget;
        socketWidget = nullptr;
    }
}


void VideoWidget::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    if (!currentFrame.isNull()) {
        painter.drawImage(0, 0, currentFrame);
    }
}